{
 "metadata": {
  "name": "",
  "signature": "sha256:7d273a9426844c2ad18a60238dc7bac77baa0b82e64c9408430d9dc703f02067"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "from IPython.html.widgets import interact\n",
      "from IPython.display import display"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 17
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from sympy import *\n",
      "init_printing(use_latex='mathjax')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "x = Symbol('x')\n",
      "y = Symbol('y')\n",
      "z = Symbol('z')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 18
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "def factorit(n):\n",
      "    display(Eq(x**n-1, factor(x**n-1)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "factorit(12)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$x^{12} - 1 = \\left(x - 1\\right) \\left(x + 1\\right) \\left(x^{2} + 1\\right) \\left(x^{2} - x + 1\\right) \\left(x^{2} + x + 1\\right) \\left(x^{4} - x^{2} + 1\\right)$$"
       ],
       "metadata": {},
       "output_type": "display_data",
       "text": [
        " 12                       \u239b 2    \u239e \u239b 2        \u239e \u239b 2        \u239e \u239b 4    2    \u239e\n",
        "x   - 1 = (x - 1)\u22c5(x + 1)\u22c5\u239dx  + 1\u23a0\u22c5\u239dx  - x + 1\u23a0\u22c5\u239dx  + x + 1\u23a0\u22c5\u239dx  - x  + 1\u23a0"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "interact(factorit, n=(2,20));"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$x^{5} - 1 = \\left(x - 1\\right) \\left(x^{4} + x^{3} + x^{2} + x + 1\\right)$$"
       ],
       "metadata": {},
       "output_type": "display_data",
       "text": [
        " 5               \u239b 4    3    2        \u239e\n",
        "x  - 1 = (x - 1)\u22c5\u239dx  + x  + x  + x + 1\u23a0"
       ]
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "\u4e09\u89d2\u51fd\u6570\u7b80\u5316"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from sympy import simplify, cos, sin\n",
      "a = (x + x**2)/(x*sin(y)**2 + x*cos(y)**2)\n",
      "a"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$\\frac{x^{2} + x}{x \\sin^{2}{\\left (y \\right )} + x \\cos^{2}{\\left (y \\right )}}$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 20,
       "text": [
        "         2           \n",
        "        x  + x       \n",
        "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n",
        "     2           2   \n",
        "x\u22c5sin (y) + x\u22c5cos (y)"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "simplify(a)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$x + 1$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "x + 1"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " M = Matrix(([1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 23
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "M"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$\\left[\\begin{matrix}1 & 2 & 3 & 4\\\\5 & 6 & 7 & 8\\\\9 & 10 & 11 & 12\\\\13 & 14 & 15 & 16\\end{matrix}\\right]$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 24,
       "text": [
        "\u23a11   2   3   4 \u23a4\n",
        "\u23a2              \u23a5\n",
        "\u23a25   6   7   8 \u23a5\n",
        "\u23a2              \u23a5\n",
        "\u23a29   10  11  12\u23a5\n",
        "\u23a2              \u23a5\n",
        "\u23a313  14  15  16\u23a6"
       ]
      }
     ],
     "prompt_number": 24
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      " N = Matrix(([6,3,1,4],[7,6,1,1],[1,10,11,12],[13,14,15,16]))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 25
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "N"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$\\left[\\begin{matrix}6 & 3 & 1 & 4\\\\7 & 6 & 1 & 1\\\\1 & 10 & 11 & 12\\\\13 & 14 & 15 & 16\\end{matrix}\\right]$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 26,
       "text": [
        "\u23a16   3   1   4 \u23a4\n",
        "\u23a2              \u23a5\n",
        "\u23a27   6   1   1 \u23a5\n",
        "\u23a2              \u23a5\n",
        "\u23a21   10  11  12\u23a5\n",
        "\u23a2              \u23a5\n",
        "\u23a313  14  15  16\u23a6"
       ]
      }
     ],
     "prompt_number": 26
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "\u77e9\u9635\u4e58\u6cd5"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "M,N,M*N"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$\\begin{pmatrix}\\left[\\begin{matrix}1 & 2 & 3 & 4\\\\5 & 6 & 7 & 8\\\\9 & 10 & 11 & 12\\\\13 & 14 & 15 & 16\\end{matrix}\\right], & \\left[\\begin{matrix}6 & 3 & 1 & 4\\\\7 & 6 & 1 & 1\\\\1 & 10 & 11 & 12\\\\13 & 14 & 15 & 16\\end{matrix}\\right], & \\left[\\begin{matrix}75 & 101 & 96 & 106\\\\183 & 233 & 208 & 238\\\\291 & 365 & 320 & 370\\\\399 & 497 & 432 & 502\\end{matrix}\\right]\\end{pmatrix}$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 12,
       "text": [
        "\u239b\u23a11   2   3   4 \u23a4, \u23a16   3   1   4 \u23a4, \u23a175   101  96   106\u23a4\u239e\n",
        "\u239c\u23a2              \u23a5  \u23a2              \u23a5  \u23a2                  \u23a5\u239f\n",
        "\u239c\u23a25   6   7   8 \u23a5  \u23a27   6   1   1 \u23a5  \u23a2183  233  208  238\u23a5\u239f\n",
        "\u239c\u23a2              \u23a5  \u23a2              \u23a5  \u23a2                  \u23a5\u239f\n",
        "\u239c\u23a29   10  11  12\u23a5  \u23a21   10  11  12\u23a5  \u23a2291  365  320  370\u23a5\u239f\n",
        "\u239c\u23a2              \u23a5  \u23a2              \u23a5  \u23a2                  \u23a5\u239f\n",
        "\u239d\u23a313  14  15  16\u23a6  \u23a313  14  15  16\u23a6  \u23a3399  497  432  502\u23a6\u23a0"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "M+N"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$\\left[\\begin{matrix}7 & 5 & 4 & 8\\\\12 & 12 & 8 & 9\\\\10 & 20 & 22 & 24\\\\26 & 28 & 30 & 32\\end{matrix}\\right]$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 27,
       "text": [
        "\u23a17   5   4   8 \u23a4\n",
        "\u23a2              \u23a5\n",
        "\u23a212  12  8   9 \u23a5\n",
        "\u23a2              \u23a5\n",
        "\u23a210  20  22  24\u23a5\n",
        "\u23a2              \u23a5\n",
        "\u23a326  28  30  32\u23a6"
       ]
      }
     ],
     "prompt_number": 27
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "\u77e9\u9635\u5206\u89e3"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "A = Matrix([[1,1,1],[1,1,3],[2,3,4]])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 13
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "Q, R = A.QRdecomposition()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 14
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Q,R"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$\\begin{pmatrix}\\left[\\begin{matrix}\\frac{\\sqrt{6}}{6} & - \\frac{\\sqrt{3}}{3} & - \\frac{\\sqrt{2}}{2}\\\\\\frac{\\sqrt{6}}{6} & - \\frac{\\sqrt{3}}{3} & \\frac{\\sqrt{2}}{2}\\\\\\frac{\\sqrt{6}}{3} & \\frac{\\sqrt{3}}{3} & 0\\end{matrix}\\right], & \\left[\\begin{matrix}\\sqrt{6} & \\frac{4 \\sqrt{6}}{3} & 2 \\sqrt{6}\\\\0 & \\frac{\\sqrt{3}}{3} & 0\\\\0 & 0 & \\sqrt{2}\\end{matrix}\\right]\\end{pmatrix}$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 15,
       "text": [
        "\u239b\u23a1  ___     ___      ___ \u23a4  \u23a1           ___         \u23a4\u239e\n",
        "\u239c\u23a2\u2572\u2571 6   -\u2572\u2571 3    -\u2572\u2571 2  \u23a5  \u23a2  ___  4\u22c5\u2572\u2571 6       ___\u23a5\u239f\n",
        "\u239c\u23a2\u2500\u2500\u2500\u2500\u2500  \u2500\u2500\u2500\u2500\u2500\u2500\u2500  \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u23a5, \u23a2\u2572\u2571 6   \u2500\u2500\u2500\u2500\u2500\u2500\u2500  2\u22c5\u2572\u2571 6 \u23a5\u239f\n",
        "\u239c\u23a2  6       3        2   \u23a5  \u23a2          3            \u23a5\u239f\n",
        "\u239c\u23a2                       \u23a5  \u23a2                       \u23a5\u239f\n",
        "\u239c\u23a2  ___     ___      ___ \u23a5  \u23a2          ___          \u23a5\u239f\n",
        "\u239c\u23a2\u2572\u2571 6   -\u2572\u2571 3     \u2572\u2571 2  \u23a5  \u23a2        \u2572\u2571 3           \u23a5\u239f\n",
        "\u239c\u23a2\u2500\u2500\u2500\u2500\u2500  \u2500\u2500\u2500\u2500\u2500\u2500\u2500   \u2500\u2500\u2500\u2500\u2500 \u23a5  \u23a2  0     \u2500\u2500\u2500\u2500\u2500      0   \u23a5\u239f\n",
        "\u239c\u23a2  6       3        2   \u23a5  \u23a2          3            \u23a5\u239f\n",
        "\u239c\u23a2                       \u23a5  \u23a2                       \u23a5\u239f\n",
        "\u239c\u23a2  ___     ___          \u23a5  \u23a2                   ___ \u23a5\u239f\n",
        "\u239c\u23a2\u2572\u2571 6    \u2572\u2571 3           \u23a5  \u23a3  0       0      \u2572\u2571 2  \u23a6\u239f\n",
        "\u239c\u23a2\u2500\u2500\u2500\u2500\u2500   \u2500\u2500\u2500\u2500\u2500      0   \u23a5                           \u239f\n",
        "\u239d\u23a3  3       3            \u23a6                           \u23a0"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "\u4e0d\u5b9a\u79ef\u5206"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "integrate(x**2 * exp(x) * cos(x), x)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "$$\\frac{x^{2} e^{x}}{2} \\sin{\\left (x \\right )} + \\frac{x^{2} e^{x}}{2} \\cos{\\left (x \\right )} - x e^{x} \\sin{\\left (x \\right )} + \\frac{e^{x}}{2} \\sin{\\left (x \\right )} - \\frac{e^{x}}{2} \\cos{\\left (x \\right )}$$"
       ],
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 16,
       "text": [
        " 2  x           2  x                         x           x       \n",
        "x \u22c5\u212f \u22c5sin(x)   x \u22c5\u212f \u22c5cos(x)      x          \u212f \u22c5sin(x)   \u212f \u22c5cos(x)\n",
        "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 - x\u22c5\u212f \u22c5sin(x) + \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 - \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n",
        "     2              2                           2           2    "
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 16
    }
   ],
   "metadata": {}
  }
 ]
}